package com.neusoft.elmboot.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.VirtualWallet;
import com.neusoft.elmboot.po.VirtualWalletTrans;
import com.neusoft.elmboot.service.VirtualWalletService;

@RestController
@RequestMapping("/wallets")
public class VirtualWalletController {

    @Autowired
    private VirtualWalletService virtualWalletService;

    // 根据钱包ID获取虚拟钱包对象
    @GetMapping("/{walletId}")
    public VirtualWallet getVirtualWallet(@PathVariable Long walletId) {
        return virtualWalletService.getVirtualWallet(walletId);
    }

    // 根据钱包ID获取钱包余额
    @GetMapping("/{walletId}/balance")
    public BigDecimal getBalance(@PathVariable Long walletId) {
        return virtualWalletService.getBalance(walletId);
    }

    // 从钱包中扣除指定金额
    @PostMapping("/{walletId}/debit")
    public void debit(@PathVariable Long walletId, @RequestParam BigDecimal amount) {
        virtualWalletService.debit(walletId, amount);
    }

    // 向钱包中增加指定金额
    @PostMapping("/{walletId}/credit")
    public void credit(@PathVariable Long walletId, @RequestParam BigDecimal amount) {
        virtualWalletService.credit(walletId, amount);
    }

    // 解冻钱包中的金额
    @PostMapping("/{walletId}/unfreeze")
    public void unfreeze(@PathVariable Long walletId, @RequestParam BigDecimal amount) {
        virtualWalletService.unfreeze(walletId, amount);
    }

    // 实现钱包之间的转账操作
    @PostMapping("/transfer")
    public void transfer(@RequestParam Long fromWalletId, @RequestParam Long toWalletId, @RequestParam BigDecimal amount) {
        virtualWalletService.transfer(fromWalletId, toWalletId, amount);
    }

     // 实现钱包之间的转账操作(透支支付)
    @PostMapping("/transferOverdraft")
    public void transferOverdraft(@RequestParam Long fromWalletId, @RequestParam Long toWalletId, @RequestParam BigDecimal amount,Integer repaymentDeadline) {
        virtualWalletService.transfer(fromWalletId, toWalletId, amount,repaymentDeadline);
    }

    // 用户确认收货，商家解冻收入金额
    @PostMapping("/transaction/{transactionId}/confirm")
    public void confirm(@PathVariable Long transactionId) {
        virtualWalletService.confirm(transactionId);
    }

    // 用户充值
    @PostMapping("/{walletId}/deposit")
    public void deposit(@PathVariable Long walletId, @RequestParam BigDecimal amount) {
        virtualWalletService.deposit(walletId, amount);
    }

    // 创建虚拟钱包
    @PostMapping("/create")
    public int createVirtualWallet(@RequestParam String userId) {
        return virtualWalletService.createVirtualWallet(userId);
    }

    // 根据用户ID获取虚拟钱包对象
    @GetMapping("/wallet/{userId}")
    public VirtualWallet getVirtualWalletByUserId(@PathVariable String userId) {
        return virtualWalletService.getVirtualWalletByUserId(userId);
    }

    // 列举用户的交易流水
    @GetMapping("/{walletId}/transactions")
    public List<VirtualWalletTrans> listTransactions(@PathVariable Long walletId) {
        return virtualWalletService.listTransactions(walletId);
    }

    // 用户还款
    @PostMapping("/{walletId}/decreaseOverdraftAmount")
    public void decreaseOverdraftAmount(@PathVariable Long walletId, @RequestParam BigDecimal amount) {
        virtualWalletService.decreaseOverdraftAmount(walletId, amount);
    }

    // 用户提现
    @PostMapping("/{walletId}/withdraw")
    public void withdraw(@PathVariable Long walletId, @RequestParam BigDecimal amount) {
        virtualWalletService.withdraw(walletId, amount);
    }
}